package com.chentf.controller;

import com.alibaba.fastjson.JSONObject;
import com.chentf.entity.Product;
import com.chentf.feginclient.ProductClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chentengfei
 * @desc ...
 * @date 2021-09-19 19:49:07
 */
@RestController
public class CategoryController {
    private static final Logger log = LoggerFactory.getLogger(CategoryController.class);

    @Autowired
    private ProductClient productClient;

    @GetMapping("/clientTest07")
    public String clientTest07(){
        String result = productClient.findByCategoryIdAndPage(1, 5, 1);
        System.out.println(result);
        //自定义json反序列化   对象转为json 序列化   on字符串转为对象
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.out.println(jsonObject.get("total"));
        Object rows = jsonObject.get("rows");
        System.out.println(rows);
        //二次json反序列化
        List<Product> products = JSONObject.parseArray(rows.toString(),Product.class);
        products.forEach(product -> log.info("product:{}",product));
        return result;
    }

    @GetMapping("/clientTest06")
    public void clientTest06(){
        List<Product> products = productClient.findByCategoryId(21);
        products.forEach(product -> log.info("product:{}",product));
    }

    //声明调用商品服务中test05接口,传递一个id,获取Product对象
    @GetMapping("/clientTest05")
    public Product clientTest05(){
        Product product = productClient.test05(21);
        return product;
    }

    //声明调用商品服务中test04接口,传递一个List集合
    @GetMapping("/clientTest04")
    public String clientTest04(){
        String s = productClient.test04(new String[]{"21", "22", "23"});
        return s;
    }

    //声明调用商品服务中test03接口,传递一个数组
    @GetMapping("/clientTest03")
    public String clientTest03(){
        String s = productClient.test03(new String[]{"21", "22", "23"});
        return s;
    }

    //声明调用商品服务中test02接口,传递一个对象
    @GetMapping("/clientTest02")
    public String clientTest02(){
        String result = productClient.test02(new Product(21, "chentf", new BigDecimal(1000.00), LocalDateTime.now()));
        return result;
    }

    //声明调用商品服务中test01接口,路径传递参数
    @GetMapping("/clientTest01")
    public String clientTest01(){
        String result = productClient.test01("21","chentf");
        return result;
    }

    //定义一个接受零散类型参数接口,调用test接口
    @GetMapping("/clientTest")
    public String clientTest(){
        String result = productClient.test("chentf",23);
        return result;
    }

    @GetMapping("/category")
    public String category(){
        System.out.println("类别接口调用商品接口...");
        String result = productClient.product();
        return result;
    }

}
